package cn.com.controler.Admin;
import cn.com.entity.Admin;

import java.util.Date;
import java.util.List;

public class AdminDAO {
    //根据id查询
    public List<Admin> selectbyid(int id){
        String str = "select *from admin where AdminId = ?";
        Object []arr = new Object[]{id};
        return AdminDAOTemplete.selectdata(str,arr);
    }
    //用户名密码查询，普通管理员登录
    public List<Admin> loginselect(String username,String password){
        String str = "select *from Admin where AdminUsername = ? and AdminPassword = ? and AdminRoleType = 0";
        Object []arr = new Object[]{username,password};
        return AdminDAOTemplete.selectdata(str,arr);
    }
    //用户名密码查询，超级管理员登录
    public List<Admin> cjloginselect(String username,String password){
        String str = "select *from Admin where AdminUsername = ? and AdminPassword = ? and AdminRoleType = 1";
        Object []arr = new Object[]{username,password};
        return AdminDAOTemplete.selectdata(str,arr);
    }
    //查询登录状态码
    public int getroletypestate(int id){
        String str = "select *from Admin where AdminId = ?";
        Object []arr = new Object[]{id};
        List selectdata = AdminDAOTemplete.selectdata(str, arr);
        Admin admin = (Admin) selectdata.get(0);
        return admin.getAdminRoleType();
    }
    //新增管理员
    public int addadmin(Admin admin){
        String str = "insert into admin values(null,?,?,?,?,0)";
        Object arr[] = new Object[]{admin.getAdminUsername(),admin.getAdminPassword(),admin.getAdminEmail(),new Date()};
        return  AdminDAOTemplete.insertdata(str,arr);
    }

    //查询所有普管
    public List<Admin> selectalladmin(){
        String str = "select *from Admin where AdminRoleType=0";
        Object []arr = new Object[]{};
        return AdminDAOTemplete.selectdata(str,arr);
    }

    //查询所有
    public List<Admin> selectall(){
        String str = "select *from Admin order by AdminRoleType desc";
        Object []arr = new Object[]{};
        return AdminDAOTemplete.selectdata(str,arr);
    }

    //根据id删除
    public int deletebyid(int id){
        String str = "delete from admin where AdminId = ?";
        Object []arr = new Object[]{id};
        return AdminDAOTemplete.insertdata(str,arr);
    }

    //根据id升级超管
    public int shengjibyid(int id){
        String str = "update admin set AdminRoleType = 1 where AdminId = ?";
        Object []arr = new Object[]{id};
        return AdminDAOTemplete.insertdata(str,arr);
    }

    //根据id修改用户名，密码，邮箱地址
    public int updateupe(Integer id ,String username,String password,String email){
        String str = "update admin set AdminUsername = ?,AdminPassword = ?,AdminEmail = ? where AdminId = ?";
        Object []arr = new Object[]{username,password,email,id};
        return AdminDAOTemplete.insertdata(str,arr);
    }
}
